<template>
    <div class="basis-info">
        <Form ref="productPlan" :model="productPlan" :rules="productPlanFormValidate"
              v-if="!(parentParam.status=='view' && productPlan.discountPlanName=='' && productPlan.extrasPlanName=='' && productPlan.riskPlanName=='' && productPlan.launchPlanName=='' && productPlan.financePlanName=='' )"
              :label-width="250">
            <div>
                <Alert>
                    <h2>规则方案</h2>
                </Alert>
            </div>
            <div v-if="discountType=='1' ||discountType=='2'">
                <Row>
                    <Col span="20">
                        <Form-item label="产品贴息方案名称" prop="discountPlanName">
                            <div style="display:flex;">
                                <Input v-model="productPlan.discountPlanName" :readonly="true"
                                       :clearable="parentParam.status!='view'"
                                       style="margin-right:10px;width:30%"/>
                                <Button type="primary"  icon="ios-create" v-show="parentParam.status!='view'" @click="selectPlan('discount')">
                                    选择
                                </Button>
                                &nbsp;&nbsp;&nbsp;
                                <Button icon="ios-add" v-show="parentParam.status!='view'" @click="addNewTab('discount')">
                                    打开
                                </Button>
                            </div>
                        </Form-item>
                    </Col>
                </Row>
            </div>
            <div v-if="isAddLoan=='1' ||isAddLoan=='2'">
                <Row>
                    <Col span="20">
                        <Form-item label="附加贷方案名称" prop="extrasPlanName">
                            <div style="display:flex;">
                                <Input v-model="productPlan.extrasPlanName" :readonly="true"
                                       :clearable="parentParam.status!='view'"
                                       style="margin-right:10px;width:30%"/>
                                <Button type="primary" v-show="parentParam.status!='view'" @click="selectPlan('extras')" icon="ios-create">
                                    选择
                                </Button>
                                &nbsp;&nbsp;&nbsp;
                                <Button icon="ios-add" v-show="parentParam.status!='view'" @click="addNewTab('extras')">
                                    打开
                                </Button>
                            </div>
                        </Form-item>
                    </Col>
                </Row>
            </div>
            <div v-if="isRiskPricing=='1' ||isRiskPricing=='2'">
                <Row>
                    <Col span="20">
                        <Form-item label="风险定价方案名称" prop="riskPlanName">
                            <div style="display:flex;">
                                <Input v-model="productPlan.riskPlanName" :readonly="true"
                                       :clearable="parentParam.status!='view'"
                                       style="margin-right:10px;width:30%"/>
                                <Button type="primary" v-show="parentParam.status!='view'" @click="selectPlan('risk')" icon="ios-create">
                                    选择
                                </Button>
                                &nbsp;&nbsp;&nbsp;
                                <Button icon="ios-add" v-show="parentParam.status!='view'" @click="addNewTab('risk')">
                                    打开
                                </Button>
                            </div>
                        </Form-item>
                    </Col>
                </Row>
            </div>
            <div v-if="templateCategory=='1'||parentParam.templateCategory=='1'">
                <Row>
                    <Col span="20" v-if="!(parentParam.status=='view' && productPlan.launchPlanName=='')">
                        <Form-item label="产品投放方案名称" prop="launchPlanName">
                            <div style="display:flex;">
                                <Input v-model="productPlan.launchPlanName" :readonly="true"
                                       :clearable="parentParam.status!='view'"
                                       style="margin-right:10px;width:30%"/>
                                <Button type="primary" v-show="parentParam.status!='view'" @click="selectPlan('launch')" icon="ios-create">
                                    选择
                                </Button>
                                &nbsp;&nbsp;&nbsp;
                                <Button icon="ios-add" v-show="parentParam.status!='view'" @click="addNewTab('launch')">
                                    打开
                                </Button>
                            </div>
                        </Form-item>
                    </Col>
                </Row>
            </div>
            <Row>
                <Col span="20" v-if="!(parentParam.status=='view' && productPlan.financePlanName=='')">
                    <Form-item label="融资校验方案名称" prop="financePlanName">
                        <div style="display:flex;">
                            <Input v-model="productPlan.financePlanName" :readonly="true"
                                   :clearable="parentParam.status!='view'"
                                   style="margin-right:10px;width:30%"/>
                            <Button type="primary" v-show="parentParam.status!='view'" @click="selectPlan('finance')" icon="ios-create">
                                选择
                            </Button>
                            &nbsp;&nbsp;&nbsp;
                            <Button icon="ios-add" v-show="parentParam.status!='view'" @click="addNewTab('finance')">
                                打开
                            </Button>
                        </div>
                    </Form-item>
                </Col>
            </Row>
        </Form>
        <div v-if="isShowModal">
            <rui-page :defines="defines">
                <Modal
                    v-model="isShowModal"
                    :title="modalTitle"
                    :width="1200"
                    :closable="false"
                    :mask-closable="false"
                >
                    <div>
                        <rui-query
                            :label-width="120"
                            ref="searchForm"
                            :query="queryData"
                            :query-rows="searchFormOptions"
                        >
                        </rui-query>
                        <Row>
                            <Alert show-icon>
                                已选择 <span class="select-count">{{selectCount}}</span> 项
                                <a class="select-clear" @click="clearSelectAll">清空</a>
                            </Alert>
                        </Row>
                        <rui-table
                            :defineId="defineId"
                            :fixedLeft="['action']"
                            :showIndex="false"
                            :select="true"
                            :columns="columns"
                            :slots="[{key:'status',slot:'status'}]"
                            @loadDatas="queryData"
                            @on-selection-change="changeSelect"
                            ref-query="searchForm"
                            ref="table"
                        >
                            <template slot-scope="{ row, index }" slot="status">
                                <div>
                                    <tag v-if="row.status=='1'" color="success" size="default">有效</tag>
                                    <tag v-if="row.status=='0'" color="error" size="default">失效</tag>
                                </div>
                            </template>
                        </rui-table>
                    </div>
                    <div slot="footer">
                        <Button size="small" @click="isShowModal = false">取消</Button>
                        <Button type="primary" size="small" @click="save">确定</Button>
                    </div>
                </Modal>
            </rui-page>
        </div>
    </div>
</template>
<script>
    //附加方案查询
    import {getExtrasProgramListData} from "@/projects/afs-core-business/api/afs-product/product/extrasProgram";
    //贴息方案查询
    import {getDiscountPlanList} from "@/projects/afs-core-business/api/afs-product/product/discountPlan";
    //投放方案查询
    import {getLaunchPlanList} from "@/projects/afs-core-business/api/afs-product/product-plan-select/launchPlan";
    //融资校验方案查询
    import {getFinancePlanList} from "@/projects/afs-core-business/api/afs-product/product-plan-select/financePlan";
    //风险定价方案查询
    import {getRiskPlanList} from "@/projects/afs-core-business/api/afs-product/product-plan-select/riskPlan";

    //贴息方案查询
    import discountPlanDefine from "@/projects/afs-core-business/pages/product/discount-plan/define/discount-plan-define";
    //附加方案查询
    import defineExtrasProgram from "@/projects/afs-core-business/pages/product/extras-program/define/extras-program-define";
    //投放方案查询
    import definePlanLaunch from "@/projects/afs-core-business/pages/product/launch-plan/launch-plan-define";
    //风险定价方案查询
    import defineRiskPlan from "@/projects/afs-core-business/pages/product/risk-plan/risk-plan-define";
    //融资校验方案查询
    import defineFinancePlan from "@/projects/afs-core-business/pages/product/finance-plan/finance-plan-define";

    export default {
        name: "product-scheme-selection",
        props: {
            //首次新增父集传输的值
            parentParam: {
                Object,
            },
            //产品方案
            productPlan: {
                type: Object,
                required: true
            },
            //利率类型
            rateType: {
                type: String,
                require: true
            },
            //贴息类别
            discountType: {
                type: String,
                require: true
            },
            //是否风险定价
            isRiskPricing: {
                type: String,
                require: true
            },
            //是否附加贷
            isAddLoan: {
                type: String,
                require: true
            },
            //模板类别
            templateCategory: {
                type: String,
                require: true
            }

        },
        data() {
            return {
                productPlanFormValidate: {
                    discountPlanName: [
                        {required: true, message: "贴息方案名称不能为空", trigger: "change"}
                    ],
                    extrasPlanName: [
                        {required: true, message: "附加贷方案名称不能为空", trigger: "change"}
                    ],
                    riskPlanName: [
                        {required: true, message: "风险定价方案不能为空", trigger: "change"}
                    ],
                    // launchPlanName: [
                    //     {required: true, message: "产品投放方案名称不能为空", trigger: "change"}
                    // ],
                    // financePlanName: [
                    //     {required: true, message: "融资校验方案不能为空", trigger: "change"}
                    // ],
                },
                isreadonly: false,
                extrasLoading: false,
                discountLoading: false,
                launchLoading: false,
                riskLoading: false,
                financeLoading: false,
                defineId: "",
                modalTitle: "方案选择",
                isShowModal: false,
                selectCount: 0,
                columns: "",
                name: "",
                searchFormOptions: [],
                searchDiscountFormOptions: [
                    {
                        defineId: "defineDiscountPlan", span: 6, fields: ["discountNo", "discountPlanName", "discountCalculateMethod"]
                    },
                    {
                        defineId: "defineDiscountPlan", span: 6, fields: ["discountShareMethod"],
                    },
                ],
                discountPlanColumns: [
                    'discountNo', 'discountPlanName', 'discountCalculateMethod', 'discountShareMethod'
                ],
                searchExtrasFormOptions: [
                    {
                        defineId: "defineExtrasProgram", span: 6, fields: ["extrasPlanName", "extrasProjectNames"]
                    },

                ],
                extrasPlanColumns: ['extrasPlanName', 'status', 'extrasProjectName'],
                searchLaunchFormOptions: [
                    {
                        defineId: "definePlanLaunch", span: 6, fields: ["name"]
                    },
                ],
                launchPlanColumns: [
                    'name', 'status'
                ],
                searchRiskFormOptions: [
                    {
                        defineId: "defineRiskPlan", span: 6, fields: ["name"]
                    },
                ],
                riskPlanColumns: [
                    'name', 'status'
                ],
                searchFinanceFormOptions: [
                    {
                        defineId: "defineFinancePlan", span: 6, fields: ["name"]
                    },
                ],
                financePlanColumns: [
                    'name', 'status'
                ],
                search: {
                    pageNumber: 1,
                    pageSize: 10,
                    condition: {}
                },
            };
        },
        methods: {
            //子组件校验，传递到父组件
            validateForm() {
                let flag = true
                this.$refs['productPlan'].validate(valid => {
                    if (valid) {
                        flag = true
                    } else {
                        flag = false
                    }
                })
                return flag
            },
            changeSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            queryData(queryData) {
                // console.info("queryData:" + JSON.stringify(queryData));
                this.search = queryData;
                this.search.condition.status = '1';
                // console.info("queryData:" + JSON.stringify(queryData["condition"]));
                if ('discount' == this.name) {
                    getDiscountPlanList(this.search).then(res => {
                        if (res.code === "0000") {
                            let {records, total} = res.data;
                            this.$refs.table.updateTableData(records, total);
                        }
                    });
                } else if ('extras' == this.name) {
                    getExtrasProgramListData(this.search).then(res => {
                        if (res.code === "0000") {
                            let {records, total} = res.data;
                            this.$refs.table.updateTableData(records, total);
                        }
                    });
                } else if ('launch' == this.name) {
                    getLaunchPlanList(this.search).then(res => {
                        if (res.code === "0000") {
                            let {records, total} = res.data;
                            this.$refs.table.updateTableData(records, total);
                        }
                    });
                } else if ('risk' == this.name) {
                    getRiskPlanList(this.search).then(res => {
                        if (res.code === "0000") {
                            let {records, total} = res.data;
                            this.$refs.table.updateTableData(records, total);
                        }
                    });
                } else if ('finance' == this.name) {
                    getFinancePlanList(this.search).then(res => {
                        if (res.code === "0000") {
                            let {records, total} = res.data;
                            this.$refs.table.updateTableData(records, total);
                        }
                    });
                }
            },
            addNewTab(v) {
                const router = this.$router;
                if ("discount" == v) {
                    router.push({path: 'discount'})
                } else if ('extras' == v) {
                    router.push({path: 'ExtrasProgram'})
                } else if ('launch' == v) {
                    router.push({path: 'launch'})
                } else if ('risk' == v) {
                    router.push({path: 'risk'})
                } else if ('finance' == v) {
                    router.push({path: 'finance'})
                }
            },
            clearSelectDiscount() {
                this.productPlan.discountPlanId = "";
                this.productPlan.discountPlanName = "";
            },
            selectPlan(v) {
                this.name = v;
                //贴息方案
                if ("discount" == v) {
                    this.defineId = "defineDiscountPlan";
                    this.searchFormOptions = this.searchDiscountFormOptions
                    this.columns = this.discountPlanColumns
                    this.isShowModal = true;
                } else if ("extras" == v) {
                    this.defineId = "defineExtrasProgram";
                    this.searchFormOptions = this.searchExtrasFormOptions
                    this.columns = this.extrasPlanColumns;
                    this.isShowModal = true;
                } else if ("launch" == v) {
                    this.defineId = "definePlanLaunch";
                    this.searchFormOptions = this.searchLaunchFormOptions
                    this.columns = this.launchPlanColumns;
                    this.isShowModal = true;
                } else if ("risk" == v) {
                    this.defineId = "defineRiskPlan";
                    this.searchFormOptions = this.searchRiskFormOptions
                    this.columns = this.riskPlanColumns;
                    this.isShowModal = true;
                } else if ("finance" == v) {
                    this.defineId = "defineFinancePlan";
                    this.searchFormOptions = this.searchFinanceFormOptions
                    this.columns = this.financePlanColumns;
                    this.isShowModal = true;
                }
                this.queryData(this.search);
            },
            save() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择数据！");
                    return;
                } else if (this.selectCount > 1) {
                    this.$Message.warning("您只能选择一笔数据！");
                    return;
                }
                this.selectList.forEach((e) => {
                    if ("discount" == this.name) {
                        this.productPlan.discountPlanId = e.id;
                        this.productPlan.discountPlanName = e.discountPlanName;
                    } else if ("extras" == this.name) {
                        this.productPlan.extrasPlanId = e.id;
                        this.productPlan.extrasPlanName = e.extrasPlanName;
                    } else if ("launch" == this.name) {
                        this.productPlan.launchPlanId = e.id;
                        this.productPlan.launchPlanName = e.name;
                    } else if ("risk" == this.name) {
                        this.productPlan.riskPlanId = e.id;
                        this.productPlan.riskPlanName = e.name;
                    } else if ("finance" == this.name) {
                        this.productPlan.financePlanId = e.id;
                        this.productPlan.financePlanName = e.name;
                    }
                });
                this.selectList = {};
                this.selectCount = 0;
                this.isShowModal = false;
            },
            clearSelectAll() {
                this.$refs.table.getTable().selectAll(false);
            },

            clearSelectExtras() {
                this.productPlan.extrasPlanId = "";
                this.productPlan.extrasPlanName = "";
            },
            clearSelectLaunch() {
                this.productPlan.launchPlanId = "";
                this.productPlan.launchPlanName = "";
            },
            clearSelectRisk() {
                this.productPlan.riskPlanId = "";
                this.productPlan.riskPlanName = "";
            },
            clearSelectFinance() {
                this.productPlan.financePlanId = "";
                this.productPlan.financePlanName = "";
            },
            init() {
                // this.selectDiscountSourceData();
            }

        },
        watch: {
            'discountType': {
                handler: function (val) {
                    if (val != "" && val != null && val !== '1' && val !== '2') {
                        this.productPlan.discountPlanId = "";
                        this.productPlan.discountPlanName = "";
                    }

                }
            },
            'isAddLoan': {
                handler: function (val) {
                    if (val != "" && val != null && val !== '1' && val !== '2') {
                        this.productPlan.extrasPlanId = "";
                        this.productPlan.extrasPlanName = "";
                    }
                }
            },
            'isRiskPricing': {
                handler: function (val) {
                    if (val != "" && val != null && val !== '1' && val !== '2') {
                        this.productPlan.riskPlanId = "";
                        this.productPlan.riskPlanName = "";
                    }
                }
            },
        },
        created() {
            let {status} = this.parentParam;
            this.status = status;
            if ('3' == status) {
                this.isreadonly = true;
            }

        },
        mounted() {
            this.init();
        },
        computed: {
            defines() {
                return [
                    {
                        id: "defineDiscountPlan",
                        fields: discountPlanDefine
                    },
                    {
                        id: "defineExtrasProgram",
                        fields: defineExtrasProgram
                    },
                    {
                        id: "definePlanLaunch",
                        fields: definePlanLaunch
                    },
                    {
                        id: "defineRiskPlan",
                        fields: defineRiskPlan
                    },
                    {
                        id: "defineFinancePlan",
                        fields: defineFinancePlan
                    },
                ];
            }
        },
    };
</script>

<style scoped>
    .common_br {
        background-color: #f7f7f7;
        border-radius: 3px;
        border: 1px solid #dcdee2;
    }
    .common_content {
        background-color: white;
        padding: 16px;
    }
    .basis-info>>>.ivu-input[disabled]{
        color: black;
    }
    .basis-info>>>.ivu-select-disabled .ivu-select-selected-value{
        color:#515a6e;
    }
    .basis-info>>>.ivu-input-number-input[disabled]{
        color:black;
    }
</style>
